package cn.edu.nju.contact.contacts;

import cn.edu.nju.contact.R;
import cn.edu.nju.contact.view.ContactViewActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.AnimationUtils;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class ContactItemCache implements OnGestureListener {
	private View baseView;
	private ImageView imageView;

	private ViewFlipper vf_body;
	private ViewFlipper vf_btn;

	private LinearLayout body_view_one;
	private LinearLayout body_view_two;

	private Button btn_phone;
	private Button btn_mail;
	private Button btn_intro;

	private ImageView img_pre;
	private ImageView img_next;

	private TextView tx_name;
	private TextView tx_phone;

	private Activity activity;

	private GestureDetector mGestureDetector;

	public ContactItemCache(View baseView, Activity activity) {
		this.baseView = baseView;
		this.activity = activity;
		mGestureDetector = new GestureDetector(this);
	}

	public ViewFlipper getBodyView(final String name,final String phoneNum) {
		if (vf_body == null) {
			vf_body = (ViewFlipper) baseView.findViewById(R.id.vf_body);

			tx_name = new TextView(activity);
			tx_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
			tx_name.setText(name);
			tx_name.setTextColor(Color.BLACK);
			tx_name.setTextSize(24);

			tx_phone = new TextView(activity);
			tx_phone.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tx_phone.setText(phoneNum);
			tx_phone.setTextColor(Color.BLACK);
			tx_phone.setTextSize(12);

			LinearLayout layout_one = new LinearLayout(activity);
			layout_one.setOrientation(LinearLayout.HORIZONTAL);
			layout_one.setLayoutParams(new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			layout_one.setPadding(0, 10, 0, 0);

			layout_one.addView(tx_name);
			layout_one.addView(tx_phone);

			vf_body.addView(layout_one);

			LinearLayout layout_two = new LinearLayout(activity);
			layout_two.setOrientation(LinearLayout.HORIZONTAL);
			layout_two.setLayoutParams(new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

			btn_phone = new Button(activity);
			btn_phone.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			btn_phone.setBackgroundResource(R.drawable.ic_pmenu_item_call);
			
			btn_mail = new Button(activity);
			btn_mail.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			btn_mail.setBackgroundResource(R.drawable.ic_pmenu_item_email);
			
			
			btn_intro = new Button(activity);
			btn_intro.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			btn_intro.setBackgroundResource(R.drawable.dark_ic_bottom_contacts_unslt);
			
			btn_intro.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClassName(activity, "cn.edu.nju.contact.view.ContactViewActivity");
					intent.putExtra("name", name);
					intent.putExtra("phone_num", phoneNum);
					activity.startActivity(intent);
				}
			});
			
			layout_two.addView(btn_intro);
			layout_two.addView(btn_phone);
			layout_two.addView(btn_mail);
			
			vf_body.addView(layout_two);
		}
		return vf_body;
	}

	public ViewFlipper getBtnView() {
		if (vf_btn == null) {
			vf_btn = (ViewFlipper) baseView.findViewById(R.id.vf_btn);

			img_next = new ImageView(activity);
			img_next.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			img_next.setImageResource(R.drawable.cp_multi_btn_right_normal);

			img_pre = new ImageView(activity);
			img_pre.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
			img_pre.setImageResource(R.drawable.cp_multi_btn_left_normal);

			vf_btn.addView(img_next);
			vf_btn.addView(img_pre);

			vf_btn.setOnTouchListener(new OnTouchListener() {

				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					return mGestureDetector.onTouchEvent(event);
				}
			});
			vf_btn.setLongClickable(true);
		}

		return vf_btn;
	}

	private void showPreView() {
		vf_body.setInAnimation(AnimationUtils.loadAnimation(activity,
				R.anim.slide_right_in));
		vf_body.setOutAnimation(AnimationUtils.loadAnimation(activity,
				R.anim.slide_left_in));
		vf_btn.setInAnimation(AnimationUtils.loadAnimation(activity,
				R.anim.slide_right_in));
		vf_btn.setOutAnimation(AnimationUtils.loadAnimation(activity,
				R.anim.slide_left_in));
		vf_body.showPrevious();
		vf_btn.showPrevious();
	}

	private void showNextView() {

	}

	public ImageView getImageView() {
		if (imageView == null) {
			imageView = (ImageView) baseView
					.findViewById(R.id.imageView_contact_item_photo);
		}
		return imageView;
	}

	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e2.getX() - e1.getX() > 10) {
			showNextView();
		} else if (e1.getX() - e2.getX() > 10) {
			showPreView();
		}
		return false;
	}

	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
}
